import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Iterator;
import javax.swing.JComponent;

//public class Can extends Canvas   {
public class MyComponent extends JComponent   {

	private static final long serialVersionUID = -3427109395116337819L;
	private int start;
	private  int goal;
	private  int help;
	private  int n;
	private  int hei;
	private  int wid;
	private  int i;
	private  int towerheight[] = {0,0,0};
	private  Hanoi h;
	private int pointer = 2;

	
	public void setPointer(int pointer) {	
		this.pointer = pointer;
		this.drawPointer(pointer, this.getGraphics());
	}

	public int getPointer(){
		return pointer;
	}

	MyComponent(int n, int start, int goal, int help, Hanoi h) {
		super();
		//System.out.println(n);
		this.n = n;
		this.h = h;
		this.goal = goal;
		this.start = start;
		this.help = help;
		setDoubleBuffered(true);
		this.setToolTipText("Play with left/right Cursor Keys and Spacebar.");
	}
	public void paintComponents(Graphics g){
		paintComponent(g);
	}

	protected void paintComponent(Graphics g) {		
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
		//i = getWidth()/3/2;
		i = getWidth()/4;								// Magics
		//hei = (getHeight() - (getHeight()/3))/n-5;		// DO NOT TOUCH - HIGH VOLTAGE
		hei = (getHeight() - (getHeight()/3))/n;
		wid = i;
		//System.out.println(getWidth() + " "+getHeight()+ " "+ wid + " "+hei+ " "+n);

		drawPointer(pointer, g);
		drawBar(1, g);
		drawBar(2, g);
		drawBar(3, g);
	}		
	
	public void drawBar(int x, Graphics g){									// Draws one Complete Bar at Position x
		Graphics2D gg = (Graphics2D) g;	
		//gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Iterator<Disk> u=null;
		gg.setColor(Color.WHITE);
		gg.fillRect(i*x-i/2, getHeight()/4, i, getHeight()/4*3+10);
		gg.setColor(Color.BLACK);
		gg.setStroke(new BasicStroke(getWidth()/60, BasicStroke.CAP_ROUND, BasicStroke.CAP_SQUARE));
		towerheight[x-1]=0;
		gg.drawLine(  x*i, getHeight()*1/3 ,   x*i , getHeight() );
		if (x == start)
			u = h.getStart().iterator();
		if (x == goal)
			u = h.getGoal().iterator();
		if (x == help)
			u = h.getHelp().iterator();
		if (u != null)
			while(u.hasNext())
				drawDisk(u.next().getSize(), x, g);
			
	}

	public void pickDisk(int pos, Graphics g){										// Removes First Disk from Bar at Position pos
		if (towerheight[pos-1] > 0){
			Graphics2D gg = (Graphics2D) g;
			gg.setColor(Color.WHITE);
			gg.fillRect(i*pos-i/2, getHeight()-towerheight[pos-1]*hei, i, hei);
			//gg.clearRect(i*pos-i/2, getHeight()-towerheight[pos-1]*hei, i, hei);
			gg.setColor(Color.BLACK);
			gg.setStroke(new BasicStroke(getWidth()/60, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
			towerheight[pos-1]--;
			gg.drawLine(pos*i, getHeight()-hei*towerheight[pos-1]/*-getHeight()/90*/ , i*pos , getHeight()/3);
		}
	}
		
	
	public void drawDisk(int size, int pos, Graphics gg){							//Draws a Disk with the Size size at the Postion pos
		towerheight[pos-1]++;
		Graphics2D g = (Graphics2D) gg;
		if(255-size*15 > 0)
			g.setColor(new Color(200,255-size*15,0));
		else
			if(size*10 < 255)
				g.setColor(new Color(200,0,size*10));
			else
				g.setColor(Color.CYAN);
//	Stem.out.println(i*pos-( wid-15*(n-size-1))/2);
		g.fillRoundRect(i*pos-( wid-10*(n-size))/2, getHeight()-towerheight[pos-1]*hei, wid-10*(n-size), hei,4,4);
	}
	
	public void drawPointer(int pos, Graphics g){									//Draws the Pointer on the top at Position pos
		Graphics2D gg = (Graphics2D) g;
		gg.setColor(Color.WHITE);
		gg.fillRect(0, 0, getWidth(), getHeight()/5);
		//g.clearRect(0, 0, getWidth(), getHeight()/5);
		gg.setColor(Color.RED);
		gg.setStroke(new BasicStroke(getWidth()/60, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL ));
		gg.drawLine(i*pos, 0, i*pos,getHeight()/5-10);
		//gg.fillRect(i*pos-getWidth()/80, 0, getWidth()/40, getHeight()/5);
		//g.setColor(Color.BLACK);
	}
}
